// 道具基类
class Item {
    constructor(x, y, type) {
        this.x = x;
        this.y = y;
        this.type = type;
        this.size = GAME_CONFIG.EXPERIENCE_ORB_SIZE;
        this.lifetime = 10000; // 10秒生命周期
        this.maxLifetime = this.lifetime;
        this.isDead = false;
        this.collected = false;
        
        this.setupItem(type);
    }
    
    // 根据类型设置道具属性
    setupItem(type) {
        switch(type) {
            case 'experience':
                this.color = '#FFD700';
                this.experienceValue = 10;
                this.symbol = 'XP';
                break;
                
            case 'health':
                this.color = '#FF6B6B';
                this.healAmount = 30;
                this.symbol = '♥';
                break;
                
            case 'shield':
                this.color = '#4ECDC4';
                this.shieldDuration = 5000; // 5秒护盾
                this.symbol = '⚡';
                break;
                
            case 'speed':
                this.color = '#45B7D1';
                this.speedBoost = 1.5;
                this.speedDuration = 3000; // 3秒加速
                this.symbol = '⚡';
                break;
                
            case 'minion':
                this.color = '#32CD32';
                this.symbol = 'M';
                break;
        }
    }
    
    // 更新道具
    update(deltaTime) {
        this.lifetime -= deltaTime;
        
        if (this.lifetime <= 0) {
            this.die();
        }
    }
    
    // 渲染道具
    render(ctx) {
        if (this.isDead) return;
        
        ctx.save();
        
        // 根据剩余生命周期调整透明度
        const alpha = this.lifetime / this.maxLifetime;
        ctx.globalAlpha = alpha;
        
        // 使用精灵渲染道具
        let spriteName = 'experience';
        switch(this.type) {
            case 'experience': spriteName = 'experience'; break;
            case 'health': spriteName = 'health'; break;
            case 'shield': spriteName = 'shield_item'; break;
            case 'speed': spriteName = 'speed'; break;
            case 'minion': spriteName = 'minion'; break;
        }
        
        if (window.game && window.game.spriteManager) {
            window.game.spriteManager.drawSprite(ctx, spriteName, this.x, this.y, this.size * 2, this.size * 2);
        } else {
            // 备用渲染（圆形）
            ctx.fillStyle = this.color;
            ctx.beginPath();
            ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
            ctx.fill();
            
            ctx.strokeStyle = '#ffffff';
            ctx.lineWidth = 2;
            ctx.stroke();
            
            // 绘制道具符号
            ctx.fillStyle = '#ffffff';
            ctx.font = '12px Arial';
            ctx.textAlign = 'center';
            ctx.textBaseline = 'middle';
            ctx.fillText(this.symbol, this.x, this.y);
        }
        
        // 绘制闪烁效果
        this.renderGlow(ctx);
        
        ctx.restore();
    }
    
    // 渲染发光效果
    renderGlow(ctx) {
        const time = Date.now() * 0.005;
        const glowIntensity = Math.sin(time) * 0.3 + 0.7;
        
        ctx.save();
        ctx.globalAlpha = glowIntensity * 0.3;
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.size * 1.5, 0, Math.PI * 2);
        ctx.fill();
        ctx.restore();
    }
    
    // 死亡
    die() {
        this.isDead = true;
    }
    
    // 收集道具
    collect(player) {
        if (this.collected || this.isDead) return false;
        
        this.collected = true;
        this.applyEffect(player);
        this.die();
        
        return true;
    }
    
    // 应用道具效果
    applyEffect(player) {
        switch(this.type) {
            case 'experience':
                player.gainExperience(this.experienceValue);
                break;
                
            case 'health':
                player.heal(this.healAmount);
                break;
                
            case 'shield':
                player.addShield(this.shieldDuration);
                break;
                
            case 'speed':
                player.addSpeedBoost(this.speedBoost, this.speedDuration);
                break;
                
            case 'minion':
                // 召唤小兵
                if (window.game && window.game.minionManager) {
                    window.game.minionManager.addMinion(player.x, player.y);
                }
                break;
        }
    }
    
    // 检查与玩家的碰撞
    collidesWith(player) {
        return Utils.circleCollision(
            this.x, this.y, this.size,
            player.x, player.y, player.size
        );
    }
}

// 道具生成器类
class ItemSpawner {
    constructor() {
        this.items = [];
        this.spawnTimer = 0;
        this.spawnInterval = 3000; // 3秒生成一个道具
        this.maxItems = 10;
    }
    
    // 更新道具生成器
    update(deltaTime, player) {
        this.updateSpawnTimer(deltaTime);
        this.updateItems(deltaTime, player);
        this.cleanupDeadItems();
    }
    
    // 更新生成计时器
    updateSpawnTimer(deltaTime) {
        this.spawnTimer += deltaTime;
        
        if (this.spawnTimer >= this.spawnInterval && this.items.length < this.maxItems) {
            this.spawnItem();
            this.spawnTimer = 0;
        }
    }
    
    // 生成道具
    spawnItem() {
        // 在玩家附近生成道具
        const spawnRadius = 200; // 生成半径
        const angle = Math.random() * Math.PI * 2;
        const distance = Math.random() * spawnRadius;
        
        const x = Math.cos(angle) * distance + (window.game?.player?.x || GAME_CONFIG.WORLD_WIDTH / 2);
        const y = Math.sin(angle) * distance + (window.game?.player?.y || GAME_CONFIG.WORLD_HEIGHT / 2);
        
        // 确保道具在世界范围内
        const clampedX = Utils.clamp(x, 50, GAME_CONFIG.WORLD_WIDTH - 50);
        const clampedY = Utils.clamp(y, 50, GAME_CONFIG.WORLD_HEIGHT - 50);
        
        // 随机选择道具类型
        const itemTypes = ['experience', 'health', 'shield', 'speed', 'minion'];
        const type = Utils.randomElement(itemTypes);
        
        const item = new Item(clampedX, clampedY, type);
        this.items.push(item);
    }
    
    // 更新所有道具
    updateItems(deltaTime, player) {
        this.items.forEach(item => {
            item.update(deltaTime);
            
            // 检查与玩家的碰撞
            if (item.collidesWith(player)) {
                item.collect(player);
            }
        });
    }
    
    // 清理死亡的道具
    cleanupDeadItems() {
        this.items = this.items.filter(item => !item.isDead);
    }
    
    // 渲染所有道具
    render(ctx) {
        this.items.forEach(item => item.render(ctx));
    }
    
    // 从敌人位置生成经验宝石
    spawnExperienceFromEnemy(enemy) {
        const item = new Item(enemy.x, enemy.y, 'experience');
        this.items.push(item);
    }
    
    // 重置道具生成器
    reset() {
        this.items = [];
        this.spawnTimer = 0;
    }
}

// 特效系统
class Effect {
    constructor(x, y, type) {
        this.x = x;
        this.y = y;
        this.type = type;
        this.lifetime = 1000; // 1秒特效
        this.maxLifetime = this.lifetime;
        this.isDead = false;
        
        this.setupEffect(type);
    }
    
    // 根据类型设置特效
    setupEffect(type) {
        switch(type) {
            case 'hit':
                this.color = '#FF0000';
                this.size = 20;
                break;
                
            case 'heal':
                this.color = '#00FF00';
                this.size = 15;
                break;
                
            case 'levelup':
                this.color = '#FFD700';
                this.size = 30;
                break;
        }
    }
    
    // 更新特效
    update(deltaTime) {
        this.lifetime -= deltaTime;
        
        if (this.lifetime <= 0) {
            this.die();
        }
    }
    
    // 渲染特效
    render(ctx) {
        if (this.isDead) return;
        
        ctx.save();
        
        const progress = 1 - (this.lifetime / this.maxLifetime);
        const alpha = 1 - progress;
        const scale = 1 + progress * 2;
        
        ctx.globalAlpha = alpha;
        ctx.translate(this.x, this.y);
        ctx.scale(scale, scale);
        
        // 绘制特效
        ctx.fillStyle = this.color;
        ctx.beginPath();
        ctx.arc(0, 0, this.size, 0, Math.PI * 2);
        ctx.fill();
        
        // 绘制边框
        ctx.strokeStyle = '#ffffff';
        ctx.lineWidth = 2;
        ctx.stroke();
        
        ctx.restore();
    }
    
    // 死亡
    die() {
        this.isDead = true;
    }
}

// 特效管理器
class EffectManager {
    constructor() {
        this.effects = [];
    }
    
    // 更新所有特效
    update(deltaTime) {
        this.effects.forEach(effect => effect.update(deltaTime));
        this.effects = this.effects.filter(effect => !effect.isDead);
    }
    
    // 渲染所有特效
    render(ctx) {
        this.effects.forEach(effect => effect.render(ctx));
    }
    
    // 添加特效
    addEffect(x, y, type) {
        const effect = new Effect(x, y, type);
        this.effects.push(effect);
    }
    
    // 重置特效管理器
    reset() {
        this.effects = [];
    }
} 